﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Collections;
using System.Linq;

#endregion

namespace ScrumTable.Common.Collections
{
    /// <summary>
    /// Contains a wrapper for the IEnumerable interface and provides an
    /// additional Count property.
    /// </summary>
    public class Enumerable<T> : IEnumerable<T>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the number of elements in the sequence.
        /// </summary>
        public int Count
        {
            get { return InnerData.Count(); }
        }

        /// <summary>
        /// Gets the inner (nexted) IEnumerable&lt;T&gt; instance.
        /// </summary>
        protected IEnumerable<T> InnerData { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new WrapperEnumerable instance. This constructor
        /// consumes the inner (nested) enumerable instance to wrap.
        /// </summary>
        /// <param name="nested">Specifies the inner (nested) enumeration.</param>
        public Enumerable(params T[] nested)
            : this((IEnumerable<T>)nested)
        {
        }

        /// <summary>
        /// Creates a new WrapperEnumerable instance. This constructor
        /// consumes the inner (nested) enumerable instance to wrap.
        /// </summary>
        /// <param name="nested">Specifies the inner (nested) enumeration.</param>
        public Enumerable(IEnumerable<T> nested)
        {
            PreCondition.AssertNotNull(nested, "nested");

            InnerData = nested;
        }

        /// <summary>
        /// Creates a new WrapperEnumerable instance. This constructor
        /// consumes the inner (nested) enumerable instance to wrap.
        /// </summary>
        /// <param name="nested">Specifies the inner (nested) enumeration. The elements of the
        /// enumeration must derived from T.</param>
        public Enumerable(IEnumerable nested)
        {
            PreCondition.AssertNotNull(nested, "nested");

            InnerData = nested.Cast<T>();
        }

        /// <summary>
        /// Creates a new WrapperEnumerable instance.
        /// </summary>
        protected Enumerable()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<T> GetEnumerator()
        {
            return InnerData.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
